package com.cdfc.estatemanagement.controller;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdfc.estatemanagement.common.MessageConstant;
import com.cdfc.estatemanagement.common.Result;
import com.cdfc.estatemanagement.common.StatusCode;
import com.cdfc.estatemanagement.domain.House;
import com.cdfc.estatemanagement.domain.vo.HouseQueryVo;
import com.cdfc.estatemanagement.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Win_Ethan
 */
@RestController
@RequestMapping("/house")
public class HouseController {

    @Autowired
    private HouseService houseService;

    @GetMapping("/find")
    public Result find() {
        List<House> all = houseService.list();
        return new Result(false, 200, "请求成功", all);
    }

    @GetMapping("get/{id}")
    public Result findById(@PathVariable Integer id) {
        House house = houseService.getById(id);
        return new Result(true, StatusCode.OK, MessageConstant.COMMUNITY_FIND_BY_ID_SUCCESS, house);
    }

    @PostMapping("/del")
    public Result del(@RequestBody List<Integer> ids) {
        Boolean flag = houseService.removeByIds(ids);
        return new Result(true, StatusCode.OK, MessageConstant.COMMUNITY_DELETE_SUCCESS);
    }

    /**
     * @param current         当前页
     * @param limit           每页记录数
     * @param houseSetQueryVo
     * @return
     */
    @PostMapping("findHousePage/{current}/{limit}")
    public Result findPageHosp(@PathVariable Long current,
                               @PathVariable Long limit,
                               @RequestBody(required = false) HouseQueryVo houseSetQueryVo) {

        Page<House> page = new Page<>(current, limit);
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        String name = houseSetQueryVo.getName();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        String type = houseSetQueryVo.getType();
        if (!StringUtils.isEmpty(type)) {
            wrapper.like("type", type);
        }
        String status = houseSetQueryVo.getStatus();
        if (!StringUtils.isEmpty(status)) {
            wrapper.like("status", status);
        }
        Page<House> pageHouse = houseService.page(page, wrapper);
        return new Result(true, StatusCode.OK, MessageConstant.GET_PAGE_SUCCESS, pageHouse);
    }
}
